package com.zjh.controller;

import com.zjh.pojo.User;
import com.zjh.service.user.UserService;
import com.zjh.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserService userImpl = new UserServiceImpl();

        User user = userImpl.Login(username);

        if (!user.getName().isEmpty()) {
            String ps = user.getPassword();
            if (ps.equals(password)) {
                req.getRequestDispatcher("/success.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
            }
        } else {
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        }
    }
}
